<template>
  <div class="params-list-container">
    <el-button
      size="mini"
      :style="btnStyle"
      icon="el-icon-plus"
      :disabled="!isSelectedCate"
      @click="$emit('addOrEditDialogHandler', 1)"
    >
      添加参数
    </el-button>
    <el-table :data="paramsList" style="width: 100%; margin-top: 15px" border>
      <el-table-column type="expand" class="expand">
        <template slot-scope="{ row }">
          <el-tag
            :key="vals"
            v-for="vals in row.attr_vals"
            closable
            :disable-transitions="false"
            @close="$emit('closeVlasHanlder', vals, row)"
          >
            {{ vals }}
          </el-tag>
          <el-input
            class="input-new-tag"
            v-if="inputVisible"
            v-model.trim="inputValue"
            ref="saveTagInput"
            size="small"
            @keyup.enter.native="$emit('handleInputConfirm', inputValue, row)"
            @blur="$emit('handleInputConfirm', inputValue, row)"
          >
          </el-input>
          <el-button
            v-else
            class="button-new-tag"
            size="small"
            @click="showInputHandler"
          >
            + New Tag
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="#" type="index"></el-table-column>
      <el-table-column label="参数名称" prop="attr_name"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{ row }">
          <el-button
            :style="btnStyle"
            size="mini"
            icon="el-icon-edit"
            :disabled="!isSelectedCate"
            @click="$emit('addOrEditDialogHandler', 0, row)"
          >
            编辑
          </el-button>
          <el-button
            :style="btnStyle"
            size="mini"
            icon="el-icon-delete"
            :disabled="!isSelectedCate"
            @click="$emit('deleteParamsHandler', row.attr_id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  name: 'params-list',
  props: {
    paramsList: {
      type: Array,
      default: () => [],
    },
    isSelectedCate: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      inputVisible: false, // tag与input的显示隐藏
      inputValue: '', // input的数据
    }
  },
  methods: {
    // 显示input的回调
    showInputHandler() {
      this.inputVisible = true
      this.inputValue = ''
      this.$nextTick(() => {
        this.$refs.saveTagInput.focus()
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.el-tag {
  margin: 10px 10px 0 0;
  &:first-child {
    margin-left: 10px;
  }
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
